What this code does:
Code for prepping data for importation to Google Earth, especially for making videos of animal movement paths
Code for generating GIFs of animal movement from tracking data
##### Clear Environment #####
remove(list=ls())
#### load libraries ####
library(sp)
library(rgdal)
library(rgeos)
library(adehabitatLT)
library(sf)
library(ggplot2)
library(raster)
library(move)
library(moveVis)
library(vembedr)
We’ll use Google Earth Pro to make a video of animal movement, but first we’ll prep our data in R.
First, we’ll load and view our data. This is dispersal data for a young male deer, and we will evaluate just the period of this animal’s dispersal.
#### load data ####
pts <- read.csv("Output/movement_video_tutorial_data.csv", header = T)
pts$date <- as.POSIXct(pts$date, tz = "America/Chicago")
head(pts)
## x y date dx dy dist dt
## 1 520149.9 290377.2 2017-05-13 04:01:50 49.97703 -28.79398 57.67839 14367
## 2 520199.9 290348.4 2017-05-13 08:01:17 169.92049 -13.81863 170.48146 14375
## 3 520369.8 290334.6 2017-05-13 12:00:52 -49.97725 16.01088 52.47927 14394
## 4 520319.9 290350.6 2017-05-13 16:00:46 -296.86325 89.40999 310.03538 14445
## 5 520023.0 290440.0 2017-05-13 20:01:31 100.95296 -20.21001 102.95603 14366
## 6 520123.9 290419.8 2017-05-14 00:00:57 -38.98788 15.10220 41.81066 14410
## R2n abs.angle rel.angle id burst idcollar dop FixType
## 1 34565.358 -0.52269380 1.49233812 5700 5700 24219 5.2 val. GPS-3D
## 2 57686.322 -0.08114553 0.44154827 5700 5700 24219 2.4 val. GPS-3D
## 3 146997.221 2.83156009 2.91270561 5700 5700 24219 4.0 val. GPS-3D
## 4 110388.231 2.84905143 0.01749134 5700 5700 24219 4.8 val. GPS-3D
## 5 7397.729 -0.19758051 -3.04663195 5700 5700 24219 1.2 val. GPS-3D
## 6 18603.008 2.77203330 2.96961381 5700 5700 24219 1.8 val. GPS-3D
## rel.deg flag b.screen speed_kmh
## 1 85.50468 no_flag no_flag 0.02385982
## 2 25.29885 no_flag no_flag 0.01445272
## 3 166.88574 no_flag no_flag 0.04269449
## 4 1.00218 no_flag no_flag 0.01312529
## 5 -174.55915 no_flag no_flag 0.07726738
## 6 170.14634 no_flag no_flag 0.02579992
# view plot of trajectory, with path colored by date of location
ggplot(pts, aes(x = x, y = y, color = date)) + geom_path() + theme_bw() + coord_fixed()
Next, we’ll convert our movement data to the correct coordinate reference systems (CRS).
## convert to an sf object to facilitate reprojecting coordinates
mov <- pts %>%
sf::st_as_sf(coords = c("x", "y"), crs = 6610)
## Reproject coordinates ##
mov.latlong <- st_transform(mov, crs = 4326) # convert to lat/long coordinates (for Google Earth example)
mov.nlcd <- st_transform(mov, crs = 3857) # convert to match NLCD coordinate reference system (for GIF example)
## Extract reprojected lat/long coordinates ##
coord <- data.frame(st_coordinates(mov.latlong))
colnames(coord) <- c("x", "y")
head(coord)
## x y
## 1 -89.99816 43.08611
## 2 -89.99754 43.08585
## 3 -89.99546 43.08573
## 4 -89.99607 43.08587
## 5 -89.99972 43.08668
## 6 -89.99848 43.08649
Now that we’ve prepped our data, we can write it out into the file format for use in Google Earth Pro.
We’ll write a function for creating a GPX file.
## function for writing out GPX files
writeGPX <- function(lat, lon, time, out_file) {
o <- c('<gpx version="1.1" creator="R">','<trk>','<trkseg>')
if (missing(time))
o <- c(o, paste('<trkpt lat="',lat,'" lon="',lon,'" />', sep=''))
else
o <- c(o, paste('<trkpt lat="',lat,'" lon="',lon,'"><time>',paste(gsub(' ','T', as.character(time)), 'Z', sep=''),'</time></trkpt>', sep=''))
o <- c(o, '</trkseg>', '</trk>', '</gpx>')
if (is.character(out_file) || inherits(out_file, "connection"))
cat(o, file=out_file, sep='\n') }
Next, we’ll use that function to write out our track file. We can also control the time scale of the animal movement that goes into this file. This data formatting can ultimately be used to make videos with data from a variety of sources - you just need lat/long coordinate data and time stamps.
## Google Earth will move along the track based on the time stamps in the imported file. Using the original time stamps makes the track go very
# slowly, so we'll scale it to a faster rate. You could modify this line to aim for, for example, a constant movement rate along the track.
secs <- Sys.time() + c(0, cumsum(mov.latlong$dt[1:(nrow(coord)-1)]/10000)) # scale to movement rate
writeGPX(lat = coord$y, lon = coord$x, time = secs, out_file = "test_trk_zoomin.gpx")
Open Google Earth Pro. To load data, click “File” –> “Open” and select the .gpx file we created in the last step. A pop-up box will appear; select “Create KML Tracks” and “Adjust altitudes to ground height” as shown below:
Your GPS data “track” should look like this once it’s loaded into Google Earth Pro:
You can easily change the visual style of the track. To start, expand the “GPS device” data so you can see “Tracks” - see the area in the red square in the photo below.